package org.qiandaosystem.qiandaosystemsource.mapper;

import org.apache.ibatis.annotations.*;
import org.qiandaosystem.qiandaosystemsource.entity.SignActivity;

import java.util.List;

@Mapper
public interface SignActivityMapper {
    
    @Select("SELECT * FROM sign_activities WHERE teacher_id = #{teacherId} AND deleted = false")
    List<SignActivity> findByTeacherId(Integer teacherId);
    
    @Select("SELECT * FROM sign_activities WHERE id = #{id} AND deleted = false")
    SignActivity findById(Integer id);
    
    @Select("SELECT * FROM sign_activities WHERE id = #{id} AND teacher_id = #{teacherId} AND deleted = false")
    SignActivity findByIdAndTeacherId(Integer id, Integer teacherId);
    
    @Insert("INSERT INTO sign_activities(teacher_id, type, start_time, end_time, secret) " +
            "VALUES(#{teacherId}, #{type}, #{startTime}, #{endTime}, #{secret})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(SignActivity activity);
    
    @Update("UPDATE sign_activities SET start_time = #{startTime}, end_time = #{endTime}, " +
            "secret = #{secret}, type = #{type} " +
            "WHERE id = #{id}")
    int update(SignActivity activity);
    
    @Update("UPDATE sign_activities SET deleted = true WHERE id = #{id} AND teacher_id = #{teacherId}")
    int delete(Integer id, Integer teacherId);
    @Mapper
        @Select("SELECT s.real_name AS studentName, sr.sign_time AS signTime, " +
                "CASE WHEN sr.status = 'success' THEN '已签到' ELSE '未签到' END AS signStatus " +
                "FROM students s " +
                "LEFT JOIN sign_records sr ON s.user_id = sr.student_id AND sr.activity_id = #{activityId} " +
                "WHERE s.teacher_id = #{teacherId} " +
                "ORDER BY s.real_name")
        List<Object[]> getSignRecordsForExport(@Param("activityId") Integer activityId, @Param("teacherId") Integer teacherId);

} 